layui.use(['table', 'admin', 'ax', 'func','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 绩效管理
     */
    var Formance = {
        tableId: "formanceTable"
    };

    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyWage/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');

    /**
     * 初始化表格的列
     */
    Formance.initColumn = function () {
        return [[
            {type: 'checkbox'},
            //{field: 'id', sort: true, title: ''},
            //{field: 'bh', sort: true, title: '编号'},
            {field: 'xm', sort: true, title: '姓名'},
            {field: 'zw', sort: true, title: '职位'},
            {field: 'flag', sort: true, title: '月份'},
            {field: 'dm', sort: true, title: '店名'},
            {field: 'yhk', sort: true, title: '银行卡'},
            // {field: 'cl', sort: true, title: '纯利'},
            // {field: 'jx', sort: true, title: '绩效'},
            {field: 'sf', sort: true, title: '实发'},
            {field: 'cqts', sort: true, title: '出勤天数'},
            {field: 'jb', sort: true, title: '级别'},
            {field: 'bz', sort: true, title: '备注'},
            //{field: 'rq', sort: true, title: '日期'},
            // {field: 'zb', sort: true, title: ''},
            // {field: 'lqje', sort: true, title: ''},
            // {field: 'hp', sort: true, title: ''},
            // {field: 'yh', sort: true, title: ''},
            // {field: 'dd', sort: true, title: ''},
            // {field: 'jf', sort: true, title: ''},
            //{field: 'hang', sort: true, title: ''},
            //{align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    /**
     * 点击清空按钮
     */
    Formance.reset = function () {
        $('input').val('');
        $("#status").val('');
        $("#xm").val('');
        $("#dm").val('');
        form.render();
    };

    /**
     * 点击查询按钮
     */
    Formance.search = function () {
        var queryData = {};

        queryData['dm'] = $('#dm').val();
        queryData['xm'] = $('#xm').val();
        queryData['status'] = $("#status").val();

        table.reload(Formance.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    /**
     * 审核
     */
    Formance.confirmOk = function () {
        var checkRows = table.checkStatus(Formance.tableId);
        var data = checkRows.data;
        var ids="";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要确认的数据");
        }else{
            for (var i in data){
                ids+=data[i].id + "," + data[i].sta + ";";
            }

            var ajax = new $ax(Feng.ctxPath + "/formance/confirmOk", function () {
                Feng.success("确认成功!");
                table.reload(Formance.tableId);
            }, function (data) {
                Feng.error("确认失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids",ids);
            ajax.start();
        }

    };

    /**
     * 点击批量取消
     * @param data
     */
    Formance.confirmQx = function () {
        var checkRows = table.checkStatus(Formance.tableId);
        var data = checkRows.data;
        var ids="";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要取消审核的数据");
        }else{
            for (var i in data){
                ids+=data[i].id+",";
            }
            var ajax = new $ax(Feng.ctxPath + "/formance/confirmQx", function () {
                Feng.success("取消审核成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("取消审核失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids",ids);
            ajax.start();
        }

    };

    /**
     * 弹出添加对话框
     */
    Formance.openAddDlg = function () {
        func.open({
            title: '添加绩效',
            content: Feng.ctxPath + '/formance/add',
            tableId: Formance.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      Formance.openEditDlg = function (data) {
          func.open({
              title: '修改绩效',
              content: Feng.ctxPath + '/formance/edit?=' + data,
              tableId: Formance.tableId
          });
      };

    /**
     * 导出excel按钮
     */
    Formance.exportExcel = function () {
        //获取导出Excel的数据
        var ajax = new $ax(Feng.ctxPath + "/formance/listExport");
        var condition = $("#condition").val();
        var dm = $("#dm").val();
        var status = $("#status").val();
        ajax.set("condition",condition);
        ajax.set("dm",dm);
        ajax.set("status",status);
        var result = ajax.start();
        var lists=result;
        for (index in lists) {
            var yhk = lists[index].yhk;
            lists[index].yhk = yhk + "\t";
        }
        table.exportFile(tableResult.config.id, lists, 'xls');
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Formance.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/formance/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Formance.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("", data);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Formance.tableId,
        url: Feng.ctxPath + '/formance/list',
        page: true,
        height: "full-59",
        cellMinWidth: 100,
        limit: 20,
        cols: Formance.initColumn()
    });

    // 清空按钮点击事件
    $('#btnReset').click(function () {
        Formance.reset();
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Formance.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    Formance.openAddDlg();

    });

    // 确认按钮点击事件
    $('#btnOk').click(function () {
        Formance.confirmOk();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Formance.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Formance.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Formance.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Formance.onDeleteItem(data);
        }
    });
});
